import { _decorator, Component, Node, Label, clamp01, UITransform, size, v3 } from 'cc';
import { BasicUI } from '../../Init/Basic/BasicUI';
import { GlobalConfig } from '../../Init/Config/GlobalConfig';
import { GlobalTmpData } from '../../Init/Config/GlobalTmpData';
import EventManager from '../../Init/Managers/EventManager';
import { EventTypes } from '../../Init/Managers/EventTypes';
import { AudioEnum } from '../../Init/SystemAudio/AudioEnum';
import { AudioSystem } from '../../Init/SystemAudio/AudioSystem';
import { SDKSystem, PlatformType } from '../../Init/SystemSDK/SDKSystem';
import { StorageSystem } from '../../Init/SystemStorage/StorageSystem';
import { UIEnum } from '../../Init/SystemUI/UIEnum';
import { UISystem } from '../../Init/SystemUI/UISystem';
import { GlobalEnum } from '../../Init/Config/GlobalEnum';

const { ccclass, property } = _decorator;

@ccclass('LoseUI')
export class LoseUI extends BasicUI {
    @property(Node)
    bgMask: Node = null;
    @property(Node)
    panel: Node = null;

    public show(d) {
        super.show(d);

        // 退出 Log
        EventManager.emit(EventTypes.SDKEvents.ExitStage, GlobalEnum.ExitLvState.Fail);

        AudioSystem.playEffect(AudioEnum.Lose);

        EventManager.emit(EventTypes.GameEvents.GamePause);

        UISystem.showUI(UIEnum.PlayerAssetsUI);

        this.initInfo();
        this.initBtns();
    }

    public hide(d?: any): void {
        super.hide(d);
        EventManager.emit(EventTypes.GameEvents.GameResume);
    }

    //#region ---------信息----------
    @property(Label)
    getGold: Label = null;

    protected curLv = 0;

    initInfo() {
        this.curLv = GlobalTmpData.CurLv;
    }

    //#endregion

    // #region --------按钮----------

    @property(Node)
    againBtn: Node = null;

    protected initBtns() {
        this.againBtn.active = true;
    }
    // 重来
    protected onAgainClick() {
        // Loading 过度
        UISystem.showUI(UIEnum.LoadingUI, {
            enterCb: () => {
                // 扣除次数
                const userAssets = StorageSystem.getData().userAssets;
                userAssets.daliyFreeLvCount--;
                StorageSystem.saveData();

                // 关闭当前界面
                UISystem.hideUI(UIEnum.LevelInfoUI);
                UISystem.hideUI(UIEnum.LoseUI);
                // 手动清除关卡场景
                this.emit(EventTypes.GameEvents.SetLevelManagerEnable, false);
                // 重新开始
                EventManager.emit(EventTypes.GameEvents.GameStart);
            }
        })
    }

    //关闭 -> 回到主页
    protected onCloseClick() {
        UISystem.showUI(UIEnum.LoadingUI, {
            enterCb: () => {
                UISystem.hideUI(UIEnum.LevelInfoUI);
                UISystem.hideUI(UIEnum.LoseUI);
                //重新开始 /返回首页           
                this.emit(EventTypes.GameEvents.BackHome, true);
            }
        })
    }

    //分享录屏按钮
    protected onShareClick() {
        switch (SDKSystem._curPlatform) {
            case PlatformType.WXMiniGame:
                this.onTranspondBtnClick();
                break;
            case PlatformType.TTMiniGame:
                this.onShareRecord();
                break;
            default:
                break;
        }
    }

    // 分享录屏-tt
    protected onShareRecord() {
        EventManager.emit(EventTypes.SDKEvents.ShareRecord, () => {
        });
    }

    // 转发-wx share
    protected onTranspondBtnClick() {
        let _title = '第' + this.curLv + '关！太难了,你能过吗?';

        EventManager.emit(EventTypes.SDKEvents.Share, {
            title: _title, useUrl: true,
            urlType: GlobalEnum.SharePosition.LosePage
        });

        EventManager.emit(EventTypes.SDKEvents.ShareReport, '失败界面');

    }


    //#endregion

}

